package br.uefs.dexa.rfframework.gui.impl;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

import br.uefs.dexa.rfframework.gui.interfaces.DigitalObjectPanel;
import br.uefs.dexa.rfframework.gui.interfaces.RFUserGUI;
import br.uefs.dexa.rfframework.gui.interfaces.RelevantObjectSetContainerPanel;
import br.uefs.dexa.rfframework.gui.utils.WrapLayout;
import br.unicamp.ic.lis.rfframework.objects.IDigitalObject;

public class RelevantImagePanel extends RelevantObjectSetContainerPanel {

	
	
	private static final long serialVersionUID = -8112911578491638602L;
	
	private static final int SCROLL_BAR_UNIT_INCREMENT = 35;
	
	private JPanel relevantImagesShowPanel;
	private JPanel buttonsPanel;	
	private JButton submitButton;
	private JButton finishButton;
	
	private JScrollPane relevantScrollPane;
	
	
	public RelevantImagePanel(RFUserGUI gui) 
	{
		super(gui);
	}

	@Override
	public void compose() 
	{
		relevantImagesShowPanel = new JPanel();
		buttonsPanel = new JPanel(); 
		this.submitButton = new JButton("Submit");
		this.finishButton = new JButton("Finish");
		submitButton.setEnabled(false);
		finishButton.setEnabled(false);
		configureFinishButton();
		buildPanel();
	}
	
	private void configureFinishButton()
	{
		finishButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				getGUI().userSatisfaction(true);
				finishButton.setEnabled(false);
			}
		});
	}
	
	private void buildPanel() 
	{
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		relevantImagesShowPanel.setLayout(new WrapLayout(FlowLayout.LEFT));
		
		relevantScrollPane = new JScrollPane(relevantImagesShowPanel);
		relevantScrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_BAR_UNIT_INCREMENT);
		
		buttonsPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		buttonsPanel.add(submitButton);		
		buttonsPanel.add(finishButton);
		
		add(relevantScrollPane);
		add(buttonsPanel);	
	}
	
	@Override
	public void addObject(IDigitalObject object)
	{
		super.addObject(object);
		if(objects.getSize() > 0) 
		{
			submitButton.setEnabled(true);
			finishButton.setEnabled(false);
		}
	}
	
	@Override
	public void removeObject(IDigitalObject object)
	{
		super.removeObject(object);
		if(objects.getSize() == 0)
		{
			submitButton.setEnabled(false);
			//finishButton.setEnabled(true);
		}
	}
	

	@Override
	public Component getRFProcessingTriggerComponent() 
	{
		return submitButton;
	}

	@Override
	public Container getRelevantObjectsContainer() 
	{
		return relevantImagesShowPanel;
	}
	
	@Override
	public void beforeQuerySelection()
	{
		this.submitButton.setEnabled(false);
		this.finishButton.setEnabled(false); 
	}
	
	@Override
	public void afterQuerySelection()
	{
		this.finishButton.setEnabled(true); 
	}
	
	
	@Override
	public void beforeRelevanceFeedback()
	{
		submitButton.setEnabled(true);
	}
	
	@Override
	public void afterRelevanceFeedback()
	{
		submitButton.setEnabled(false);
		finishButton.setEnabled(true);
		clearObjects();
	}

	@Override
	public void showObjectPanel(DigitalObjectPanel panel)
	{
		super.showObjectPanel(panel);
//		submitButton.setEnabled(true);
//		finishButton.setEnabled(false);
		
		JScrollBar vertical = relevantScrollPane.getVerticalScrollBar();
		vertical.setValue( vertical.getMaximum() );
	}
	

	

}
